package com.example.seakdreamandroid;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.SeekBar;

import androidx.annotation.Nullable;

import java.io.IOException;
import java.util.Timer;

public class MSPService extends Service {
    static boolean isplay;
    MediaPlayer player;
    Uri uri;
    private MusicStoryPlayerActivity activity;

    public void setActivity(MusicStoryPlayerActivity activity){
        this.activity = activity;
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        String url = intent.getStringExtra("audioUrl");
        uri = Uri.parse(url);
        player = new MediaPlayer();
        try {
            player.setDataSource(url);
        } catch (IOException e) {
            Log.e(MSPService.class.getName(),"onBind,设置资源失败");
            e.printStackTrace();
        }
        player.prepareAsync();
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                activity.handler.sendEmptyMessage(3);
            }
        });
        Finder finder = new Finder();
        finder.service = this;
        return finder;
    }

    public MSPService(){

    }

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化操作
    }

    public class  Finder extends Binder {
        public MSPService service;
        public int getDuration(){//获取总进度条
            return player.getDuration();
        }
        public int getCurrentPosition(){//获取当前进度条
            return player.getCurrentPosition();
        }
        public void setProgress(int s){//更改当前音乐进度
            player.seekTo(s);
        }
        public boolean isLooping(){
            return player.isLooping();
        }
        public void setLooping(Boolean is){
            player.setLooping(is);
        }
        public boolean isStart(){
            return player.isPlaying();
        }
        public void playM(){
            if (!player.isPlaying()){
                player.start();
                isplay = player.isPlaying();
            }else{
                player.pause();
                isplay = player.isPlaying();
            }
        }
        public void pause(){
            player.pause();
            isplay = player.isPlaying();
        }
        public void setUri(String url) throws Exception {
            Log.i(MSPService.class.getName(),"setUri()");
            uri = Uri.parse(url);
            player.reset();
            player.setDataSource(url);
            player.prepareAsync();
        }
        public String getUri(){
            return uri.toString();
        }
    }

//    @Override
//    public int onStartCommand(Intent intent, int flags, int startId) {
//        if(!player.isPlaying()){
//            player.start();
//            isplay = player.isPlaying();
//        }else{
//            player.pause();
//            isplay = player.isPlaying();
//        }
//        return super.onStartCommand(intent, flags, startId);
//    }

    @Override
    public void onDestroy() {
        player.stop();
        isplay = player.isPlaying();
        player.release();
        super.onDestroy();
        //回收
    }
}
